﻿#region "Copyright (C) Lenny Granovsky. 2011-2013"
//    This library provides extended cache capabilities to the ASP.NET applications.
//
//                Copyright (C) Lenny Granovsky. 2011-2013. 
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//    This program comes with ABSOLUTELY NO WARRANTY.
//    This is free software, and you are welcome to redistribute it
//    under certain conditions;
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace FlexiCache.Utilities
{
    internal class ErrorUtility
    {
        /// <summary>
        /// Method handles exception using standard flow to log and email the exception details.
        /// </summary>
        /// <param name="exception">Exception to handle.</param>
        public static void HandleExceptionInternally(Exception exception)
        {
            if (CachingConfiguration.Current.EnableErrorLogging)
                EventLog.WriteEntry("FlexiCache", exception.Message, EventLogEntryType.Error, 0, 0, System.Text.Encoding.Default.GetBytes(exception.StackTrace));
        }

        /// <summary>
        /// Method builds an error message by combining messages from inner exceptions to provide better information without having read the stack trace.
        /// </summary>
        /// <param name="exception">Exception to handle.</param>
        /// <returns>Returns a string with exception message.</returns>
        public static string BuildFullExceptionMessage(Exception exception)
        {
            System.Text.StringBuilder sbr = new System.Text.StringBuilder(exception.Message);
            while (exception.InnerException != null)
            {
                sbr.AppendFormat("\n{0}", exception.InnerException.Message);
                exception = exception.InnerException;
            }
            return sbr.ToString();
        }
    }
}
